//
//  UIImageView+WebImage.swift
//  Practice
//
//  Created by miaozhihui on 2017/2/9.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import SDWebImage

extension UIImageView {
    
    /// 隔离 SDWebImage 设置图像函数
    ///
    /// - Parameters:
    ///   - urlString: urlString
    ///   - placeholderImage: 占位图像
    ///   - isDrawCircle: 是否切圆
    func zh_setImage(urlString: String?, placeholderImage: UIImage?, isDrawCircle: Bool = false) {
        
        // 处理 URL
        guard let urlString = urlString, let url = URL(string: urlString) else {
            // 设置占位图像
            image = isDrawCircle ? placeholderImage?.drawCircle(size: bounds.size, bgColor: UIColor.white, sideLineColor: UIColor.darkGray) : placeholderImage?.draw(size: bounds.size)
            return
        }
        sd_setImage(with: url, placeholderImage: placeholderImage, options: [], progress: nil) {[weak self] (image, _, _, _) in
            guard let image = image else {
               self?.image = isDrawCircle ? placeholderImage?.drawCircle(size: (self?.bounds.size)!, bgColor: UIColor.white, sideLineColor: UIColor.darkGray) : placeholderImage?.draw(size: (self?.bounds.size)!)
                return
            }
            self?.image = isDrawCircle ? image.drawCircle(size: (self?.bounds.size)!, bgColor: UIColor.white, sideLineColor: UIColor.darkGray) : image.draw(size: (self?.bounds.size)!)
        }
    }
}
